<div id ="POcomplist">
<table id ='added_components' "cellspacing="0" class="sf_admin_list">
<thead>
<tr>
      <th id="sf_admin_list_th_productcode" width="25%">Product Code
              </th>
      <th id="sf_admin_list_th_description" width="35%">Description
              </th>
      <th id="sf_admin_list_th_description" width="10%">Brand
              </th>
      <th id="sf_admin_list_th_description" width="10%">Unit
              </th>
      <th id="sf_admin_list_th_description">Qty Outstanding</th>
      <th id="sf_admin_list_th_description">Qty Received</th>
</tr>
</thead>

<?php
        $po = POPeer::retrieveByPK($tally_in->getTallyno());
        if (!$po)
        {
            echo "Khong co PO";
            exit();
        }
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $po->getComponentgroupId());
        $c->addAscendingOrderByColumn(ComponentsPeer::COMPONENT_ID);
        $componentsList = ComponentsPeer::doSelect($c);
?>
<?php if (isset($tally_in->rowIds) && count($tally_in->rowIds)) {
    foreach ($tally_in->rowIds as $key=>$id) {
        $component1= ComponentPeer::retrieveByPK($id);?>
        <tr class="sf_admin_row_<?php echo $key%2?>">
            <input type="hidden" name="rowIds[]" id ="rowIds"  class = "ExistComponents" value="<? echo $component1->getId()?>"/>
            <td><?echo $component1->getProductcode()?></td>
            <td><?echo $component1->getDescription()?></td>
            <td><?echo $component1->getBrand()?></td>
            <td><?echo $component1->getComponentunit()?></td>
            <td><input type="hidden" name="qty2s[]" id ="qty2s" size="7"  value="<? echo $tally_in->qty2s[$key]?>">
            <input type="text" name="qty3s[]" id ="qty3s" size="7" disabled value="<? echo $tally_in->qty2s[$key]?>">
            </td>
            <td><input type="text" name="qtys[]" id ="qtys" size="10" value ="<?echo $tally_in->qtys[$key]?>"></td>
            
        </tr>
<?} } elseif (isset($componentsList)) {

    $rvCompGroupId = null;
    if ($tally_in->getReviseId()){
        $rvTL = TallyInPeer::retrieveByPK($tally_in->getReviseId());
        if ($rvTL)
            $rvCompGroupId = $rvTL->getComponentgroupId();
    }
    foreach ($componentsList as $key=>$comps) {
        $comp= ComponentPeer::retrieveByPK($comps->getComponentId());
        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $tally_in->getComponentgroupId());
        $c->add(ComponentsPeer::COMPONENT_ID, $comps->getComponentId());
        $TIcomps = ComponentsPeer::doSelectOne($c);

        $c = new Criteria();
        $c->add(ComponentsPeer::COMPONENTGROUP_ID, $rvCompGroupId);
        $c->add(ComponentsPeer::COMPONENT_ID, $comps->getComponentId());
        $rvTIComps = ComponentsPeer::doSelectOne($c);

        ?>
        <tr class="sf_admin_row_<?php echo $key%2?>">
            <input type="hidden" name="rowIds[]" id ="rowIds"  class = "ExistComponents" value="<? echo $comp->getId()?>"/>
            <td><?echo $comp->getProductcode()?></td>
            <td><?echo $comp->getDescription()?></td>
            <td><?echo $comp->getBrand()?></td>
            <td><?echo $comp->getComponentunit()?></td>
            <td><input type="hidden" name="qty2s[]" id ="qty2s" size="7"  value="<? echo $rvTIComps? $comps->getQty4() + $rvTIComps->getQty1(): $comps->getQty4()?>">
            <input type="text" name="qty3s[]" id ="qty3s" size="7" disabled value="<? echo $rvTIComps? $comps->getQty4() + $rvTIComps->getQty1(): $comps->getQty4()?>">
            </td>
            <td><input type="text" name="qtys[]" id ="qtys" size="10"  value ="<? echo $TIcomps? $TIcomps->getQty1(): ''?>"></td>

            
<?php }
} 

?>

</table>

</div>

<script type="text/javascript">
function BuildPOList(){
    var po = $("tally_in_tallyno");
    var pocomplist = $("POcomplist");
    pocomplist.innerHTML = "";
    var poId = po.options[po.selectedIndex].value;
    var link = '<?php echo url_for('tallyin/BuildListAjax') ?>';
    new Ajax.Updater('POcomplist', link + '?type=0&tallyno='+ poId);
}
</script>